package com.cneport.das.server.util.wmq;

import com.ibm.mq.MQQueueManager;
import com.ibm.mq.headers.MQDataException;
import com.ibm.mq.headers.pcf.PCFMessageAgent;
import com.ibm.msg.client.wmq.compat.base.internal.MQC;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Properties;

/**
 * @Desc 通过构造MQQueueManager 实现队列元数据操作。
 * @Ver 1.0
 * @Author GavinZhang
 * @Company 东方物通科技有限（北京）公司
 * @Date 2021-12-07 10:37:50
 */
@Slf4j
@Data
public class MQConnAgent {
    private MqParam mqParam;
    private PCFMessageAgent agent;

    public MQConnAgent(MqParam mqParam) {
        this.mqParam = mqParam;
        construct();
    }

    public void construct() {
        Properties props = new Properties();
        props.put(MQC.HOST_NAME_PROPERTY, mqParam.getHost());
        props.put(MQC.PORT_PROPERTY, mqParam.getPort());
        props.put(MQC.CHANNEL_PROPERTY, mqParam.getChannel());
        props.put(MQC.CCSID_PROPERTY, mqParam.getCCSID());
        props.put(MQC.USER_ID_PROPERTY, mqParam.getUsername());
        props.put(MQC.PASSWORD_PROPERTY, mqParam.getPassword());
        MQQueueManager manager = null;

        try {
            manager = new MQQueueManager(mqParam.getQueueManager(), props);
            if (manager != null) {
                agent = new PCFMessageAgent(manager);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    public void DestroyAgent(PCFMessageAgent agent) throws MQDataException {
        // Disconnect the agent.
        agent.disconnect();
        return;
    }
}
